package com.ruoyi.pcwl.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ruoyi.pcwl.domain.TravelNode;
import com.ruoyi.pcwl.domain.vo.TravelGuidesVo;
import com.ruoyi.pcwl.mapper.TravelNodeMapper;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.ruoyi.pcwl.domain.query.TravelGuidesQuery;
import com.ruoyi.pcwl.domain.TravelGuides;
import com.ruoyi.pcwl.mapper.TravelGuidesMapper;
import com.ruoyi.pcwl.service.TravelGuidesService;


/**
 * 主题旅游攻略表 业务接口实现
 */
@Service("travelGuidesService")
public class TravelGuidesServiceImpl extends ServiceImpl<TravelGuidesMapper, TravelGuides> implements TravelGuidesService {

	@Resource
	private TravelNodeMapper travelNodeMapper;

	/**
	 * 据条件查询记录数
	 */
	@Override
	public Integer findCountByParam(TravelGuidesQuery reqParam) {
		QueryWrapper<TravelGuides> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (StringUtils.isNotEmpty(reqParam.getTravelGuidesName())) {
			wrapper.like("travel_guides_name", reqParam.getTravelGuidesName());
		}
		if (StringUtils.isNotEmpty(reqParam.getTravelGuidesDetails())) {
			wrapper.like("travel_guides_details", reqParam.getTravelGuidesDetails());
		}
		if (reqParam.getCategoryId()!=null)
			wrapper.eq("category_id",reqParam.getCategoryId());
		if (StringUtils.isNotEmpty(reqParam.getCategoryName())) {
			wrapper.like("category_name", reqParam.getCategoryName());
		}
		if (StringUtils.isNotEmpty(reqParam.getTags())) {
			wrapper.like("tags", reqParam.getTags());
		}
		if (reqParam.getHot()!=null)
			wrapper.eq("hot",reqParam.getHot());
		if (StringUtils.isNotEmpty(reqParam.getStatus())) {
			wrapper.like("status", reqParam.getStatus());
		}
		if (StringUtils.isNotEmpty(reqParam.getRemark())) {
			wrapper.like("remark", reqParam.getRemark());
		}
		Long aLong = baseMapper.selectCount(wrapper);
		return aLong.intValue();
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public IPage<TravelGuides> selectPage(TravelGuidesQuery reqParam) {
		QueryWrapper<TravelGuides> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (StringUtils.isNotEmpty(reqParam.getTravelGuidesName())) {
			wrapper.like("travel_guides_name", reqParam.getTravelGuidesName());
		}
		if (StringUtils.isNotEmpty(reqParam.getTravelGuidesDetails())) {
			wrapper.like("travel_guides_details", reqParam.getTravelGuidesDetails());
		}
		if (reqParam.getCategoryId()!=null)
			wrapper.eq("category_id",reqParam.getCategoryId());
		if (StringUtils.isNotEmpty(reqParam.getCategoryName())) {
			wrapper.like("category_name", reqParam.getCategoryName());
		}
		if (StringUtils.isNotEmpty(reqParam.getTags())) {
			wrapper.like("tags", reqParam.getTags());
		}
		if (reqParam.getHot()!=null)
			wrapper.eq("hot",reqParam.getHot());
		if (StringUtils.isNotEmpty(reqParam.getStatus())) {
			wrapper.like("status", reqParam.getStatus());
		}
		if (StringUtils.isNotEmpty(reqParam.getRemark())) {
			wrapper.like("remark", reqParam.getRemark());
		}
		if (MapUtils.isNotEmpty(reqParam.getParams()))
		{
			Map<String, Object> params = reqParam.getParams();
			if (params.keySet().contains("beginTime"))
				wrapper.ge("create_date", params.get("beginTime"));
			if (params.keySet().contains("endTime"))
				wrapper.le("create_date", params.get("endTime"));
		}
		return baseMapper.selectPage(reqParam.getPage(), wrapper);
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(TravelGuides bean) {
		bean.setCreateDate(new Date());
		return baseMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<TravelGuides> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return baseMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
//	@Override
//	public Integer addOrUpdateBatch(List<TravelGuides> listBean) {
//		if (listBean == null || listBean.isEmpty()) {
//			return 0;
//		}
//		return baseMapper.insertOrUpdateBatch(listBean);
//	}

	/**
	 * 多条件更新
	 */
//	@Override
//	public Integer updateByParam(TravelGuides bean, TravelGuidesQuery param) {
//		return baseMapper.updateByParam(bean, param);
//	}

	/**
	 * 多条件删除
	 */
//	@Override
//	public Integer deleteByParam(TravelGuidesQuery param) {
//		return baseMapper.deleteByParam(param);
//	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public TravelGuidesVo getTravelGuidesById(Long id) {
		TravelGuides travelGuides = baseMapper.selectById(id);
		TravelGuidesVo travelGuidesVo = new TravelGuidesVo();
		BeanUtils.copyProperties(travelGuides,travelGuidesVo);
		List<TravelNode> travelGuidesId = travelNodeMapper.selectList(new QueryWrapper<TravelNode>().eq("travel_guides_id", travelGuides.getId()).orderByAsc("sort"));
		travelGuidesVo.setTravelNodeList(travelGuidesId);
		return travelGuidesVo;
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateTravelGuidesById(TravelGuides bean) {
		bean.setChangeDate(new Date());
		return baseMapper.updateById(bean);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteTravelGuidesById(Long id) {
		return baseMapper.deleteById(id);
	}
}
